var hgGoodsRiskCtrl  = new XFApp.client();

//自定义form表单转对象
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || null);
        } else {
            o[this.name] = this.value || null;
        }
    });
    return o;
};

XFApp.client.prototype.hgGoodsRisk = {
	hgGoodsPage : null,
	urlItems : {
		queryHgGoodsPageUrl: basePath+"/hggoods/getByPage",
		updateRiskUrl: basePath+"/hggoods/updateIsCheckByIds"
	},
	hgGoodsItemList: null,
	hgGoodsItemLength: 0,
	//加载税则库
	loadHgGoodsPage : function(){
		var params = $('#queryForm').serializeObject();

		var _this = this;
		if(!!_this.hgGoodsPage){
			_this.hgGoodsPage.page("remote",0,params);
		}else{
			_this.hgGoodsPage = $("#page").page({
				pageSize : 10,
				remote : {
					url : _this.urlItems.queryHgGoodsPageUrl,
					params : params,
					success : function (data, pageIndex) {
						var pageResult = data.result;
		        		if(null == data 
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#data_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        			hgGoodsRiskCtrl.hgGoodsRisk.hgGoodsItemList = null;
		        			hgGoodsRiskCtrl.hgGoodsRisk.hgGoodsItemLength = 0;
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#data_grid").html($("#dataTmp").render(pageResult.itemList));
		        			hgGoodsRiskCtrl.hgGoodsRisk.hgGoodsItemList = pageResult.itemList;
		        			hgGoodsRiskCtrl.hgGoodsRisk.hgGoodsItemLength = data.result.totalRecord;
		        		}
					},
					error : function(){			        	
			        	window.webDialogUtil.alert("提示","数据查询失败!");			      	  
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});
		}	
	},
	//记住批量操作的id，和批量操作的状态，用来给页面做无刷新更新
	updateIds: null,
	updateStatus: null,
	//提高商品风险等级
	updateRisk : function(id,isCheck){
		var ids = "";
		if(id){
			ids = id;
		}else{
			$('input:checkbox[name=hggoodsItem]:checked').each(function(i){
				ids += this.id+','
			});
			ids = ids.substring(0,ids.length-1);
		}
		if(!ids && this.hgGoodsItemLength == 0){
			window.webDialogUtil.alert("提示","请选择对应的记录!");
			return;	
		}
		var upOrDownStr = isCheck==1?'提高':'重置';
		var recordCount = ids.split(',').length;
		var _this = this;
		var params = $('#queryForm').serializeObject();
		params.idList = ids;
		params.isCheck = isCheck;
		_this.updateIds = ids;
		_this.updateStatus = isCheck;
		if(!ids && this.hgGoodsItemLength != 0){
			webDialogUtil.simpleConfrimAlert('此操作将会'+upOrDownStr+'总计'+this.hgGoodsItemLength+'条搜索记录的风险等级，是否继续？',function(){
				//此时没有选择记录，需要给updateIds手动复制，否则页面不会做刷新动作
				for(var k = 0;k < hgGoodsRiskCtrl.hgGoodsRisk.hgGoodsItemList.length;k ++){
					ids += hgGoodsRiskCtrl.hgGoodsRisk.hgGoodsItemList[k].id + ',';
				}
				_this.updateIds = ids.substring(0,ids.length-1);
				_this.doDpdateRisk(params);
			});
		}else if(ids){
			if(ids.indexOf(',')>=0){
				webDialogUtil.simpleConfrimAlert('是否确认'+upOrDownStr+recordCount+'条记录的风险等级',function(){
					_this.doDpdateRisk(params);
				});
			}else{
				_this.doDpdateRisk(params);
			}	
		}
		
	},
	doDpdateRisk: function(params){
		var _this = this;
		$.ajax({
			url:_this.urlItems.updateRiskUrl,
			dataType:'json',
			type:'post',
			data: params,
			success:function(data){
				if(data.isSuccess == true && data.code == 10000){
					hgGoodsRiskCtrl.hgGoodsRisk.toastMsg({title:'操作成功',type:'success'});
					hgGoodsRiskCtrl.hgGoodsRisk.updateGridRender();
				}else{
					hgGoodsRiskCtrl.hgGoodsRisk.toastMsg({title:'操作失败',text:'系统异常,请稍后再试',type:'notice'});
				}
			},
			error:function(){
				hgGoodsRiskCtrl.hgGoodsRisk.toastMsg({title:'操作失败',text:'请联系管理员',type:'error'});
			}
		});
	},
	//更新页面渲染内容
	updateGridRender: function(){
		if(!this.updateIds){
			return ;
		}
		var idArr = this.updateIds.split(',');
		for(var i = 0;i < idArr.length;i ++){
			for(var k = 0;k < this.hgGoodsItemList.length;k ++){
				if(idArr[i] == this.hgGoodsItemList[k].id){
					this.hgGoodsItemList[k].isCheck = this.updateStatus;
				}
			}
		}
		$("#data_grid").html($("#dataTmp").render(this.hgGoodsItemList));
	},
	toastMsg: function(obj){
		new PNotify({
			title: obj.title,
			text: obj.text ? obj.text : '',
			type: obj.type ? obj.type : 'notice',
			delay: 2000
		});
	},
	//选择一条记录
	selectOne : function(){
		this.changeSelAll();
	},
	//全选和反选切换
	changeSelAll : function(){
		var flag = false;
		$('input:checkbox[name=hggoodsItem]').each(function(i){
			if(this.checked){
				flag = true;
			} 
		});
		if(flag){
			$('#selectAll').html('反选');
		}else{
			$('#selectAll').html('全选');
		}
	},
	//全选
	selectAll : function(){
		$('input:checkbox[name=hggoodsItem]').each(function(i){
			if(this.checked){
				this.checked = '';
			}else{
				this.checked = 'checked';
			}
		});
		this.changeSelAll();
	},
	//为页面元素绑定事件
	event : function(){
		var _this = this;
		$('#btnSearch').on('click',function(){
			_this.loadHgGoodsPage();
		});

		$('#goods_uprisk_btn').on('click',function(){
			_this.updateRisk(null,1);
		});

		$('#goods_downrisk_btn').on('click',function(){
			_this.updateRisk(null,0);
		});

	}

};

$(function(){
	hgGoodsRiskCtrl.hgGoodsRisk.event();
	hgGoodsRiskCtrl.hgGoodsRisk.loadHgGoodsPage();
});